package com.itheima.controller;

import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Xiao
 * @date 2021/9/16
 */
@RestControllerAdvice   //标识当前类为REST风格对应的异常处理器
public class ProjectExceptionAdvice {
    //1. 处理系统异常
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException e){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员(异常对象)
        return new Result(e.getCode(),null, e.getMessage());
    }
    //2. 处理业务异常
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException e){
        return new Result(e.getCode(),null, e.getMessage());
    }
    //3. 处理非预期的其他异常
    @ExceptionHandler(Exception.class)
    public Result doException(Exception e){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员(异常对象)
        return new Result(Code.SYSTEM_UNKNOWN_ERR,null, "系统繁忙，请稍后重试！");
    }

    //统一处理所有的Exception异常
    /*@ExceptionHandler(Exception.class)
    public Result doException(Exception e){
        System.out.println("嘿嘿，异常你哪里跑？");
        return new Result(666,null, "异常你哪里跑");
    }*/
}
